package com.gzg.userservice.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class jwtUtils {
    private static final long EXPIRE_TIME= 60*60*1000;
    private static final String TOKEN_SECRET="@GZG";  //密钥盐
   //生成Token
    public  String genToken(String username,Long userId){
        String token = null;
            Date expiresAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            token = JWT.create()
                    .withIssuer("gzg-user-service")
                    .withClaim("username", username)
                    .withClaim("userId",userId)
                    .withExpiresAt(expiresAt)
                    .sign(Algorithm.HMAC256(TOKEN_SECRET));
        return token;

    }
   //验证Token
    public  DecodedJWT verify(String token){
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("gzg-user-service").build();
        return  verifier.verify(token);
    }

}
